package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gxuwz.medical.dao.BX_recordDao;
import com.gxuwz.medical.dao.Family_memberDAO;
import com.gxuwz.medical.dao.Ill_cardDao;
import com.gxuwz.medical.dao.Join_nhDao;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.bx_record.BX_record;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.family_member.Family_member;
import com.gxuwz.medical.domain.ill_card.Ill_card;
import com.gxuwz.medical.domain.join_nh.Join_nh;
import com.gxuwz.medical.exception.DbException;
import com.gxuwz.medical.tool.MyTime;

public class BX_recordServlet extends BaseServlet {
	BX_recordDao bX_recordDao = new BX_recordDao();
	String tem = null;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("m");// 动作类型参数
		if ("list".equals(m)) {
			process(request, response, "/page/bx_record/bx_record_list.jsp");
		} else if ("input".equals(m)) {
			process(request, response, "/page/bx_record/bx_record_add.jsp");
		} else if ("get".equals(m)) {
			process(request, response, "/page/bx_record/bx_record-input.jsp");
		} else if ("add".equals(m)) {
			add(request, response);
			findByRecordCode(request, response);
		} else if ("edit".equals(m)) {
			this.edit(request, response);
		} else if ("del".equals(m)) {
			this.del(request, response);
		} else if ("findByIdCard".equals(m)) {
			this.findByIdCard(request, response);
			process(request, response, "/page/bx_record/bx_record_add.jsp");
		} else if ("print".equals(m)) {
			this.finallySign(request, response);
			process(request, response, "/page/bx_record/bx_record_list.jsp");
		} else if ("sign".equals(m)) {
			this.findByRecordCode(request, response);
			process(request, response, "/page/bx_record/bx_record_list.jsp");
		} else if ("findByLike".equals(m)) {
			this.findByLike(request, response);
			process(request, response, "/page/bx_record/bx_record_findList.jsp");
		}
	}

	List<BX_record> temList = new ArrayList<BX_record>();

	/**
	 * 
	 * @Title: findByLike @Description: 多条件查询 @author 陈震洺 @param rs @param
	 *         resp @throws ServletException @throws IOException @throws
	 */
	private void findByLike(HttpServletRequest rs, HttpServletResponse resp) throws ServletException, IOException {
		try {
			temList.clear();
			// 定义一个sb来接收模糊查询的sql
			StringBuffer sb = new StringBuffer(1024);
			BX_recordDao model = new BX_recordDao();

			String id_card = rs.getParameter("IDcard");
			String Illcode = rs.getParameter("Illcode");
			String areacode = rs.getParameter("areacode");
			String startTime = rs.getParameter("startTime");
			String endTime = rs.getParameter("endTime");

			if (id_card != null && !"".equals(id_card)) {
				sb.append(" and id_card = '" + id_card + "'");
			}
			if (Illcode != null && !"".equals(Illcode)) {
				sb.append(" and chronicdis_code = '" + Illcode + "'");
			}
			if (areacode != null && !"".equals(areacode)) {
				sb.append(" and area_code = '" + areacode + "'");
			}
			if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)) {
				sb.append(" and bx_time between '" + startTime + "' AND '" + endTime + "' ");
			} else {
				if (startTime != null && !"".equals(startTime)) {
					sb.append(" and bx_time between '" + startTime + "' AND '" + MyTime.getNowForTimestamp() + "' ");
				} else if (endTime != null && !"".equals(endTime)) {
					sb.append(" and bx_time between '" + MyTime.getNowForTimestamp() + "' AND '" + endTime + "' ");
				}
			}

			List<BX_record> list = model.findByLike(sb.toString());

			temList.addAll(list);

			rs.setAttribute("list", list);

		} catch (DbException e) {
			error(rs, resp);
		}
	}

	/**
	 * 
	 * @Title: print @Description: 签字完成后 @author 陈震洺 @param rs @param
	 *         response @throws ServletException @throws IOException @throws
	 */
	public void finallySign(HttpServletRequest rs, HttpServletResponse response) throws ServletException, IOException {
		String bx_record_code = null;
		try {
			if (tem == null) {
				HttpSession session = rs.getSession();
				bx_record_code = session.getAttribute("bx_record_code").toString();
			} else {
				bx_record_code = tem;
			}
			BX_record bx_record = new BX_record();
			bx_record.edit(bx_record_code);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unused")
	public void findByRecordCode(HttpServletRequest rs, HttpServletResponse response)
			throws ServletException, IOException {
		String bx_record_code = null;
		String record_code = null;
		try {
			record_code = rs.getParameter("id");
			if (record_code != null && !record_code.isEmpty()) {
				bx_record_code = record_code;
				tem = record_code;// 放入缓存
			} else {
				HttpSession session = rs.getSession();
				bx_record_code = session.getAttribute("bx_record_code").toString();
				tem = bx_record_code;// 放入缓存
			}

			List<BX_record> list = bX_recordDao.findByKV("bx_record_code", bx_record_code);
			list.get(0).setChronicdis_code(list.get(0).getChronicdis().getIllname());
			list.get(0).setUser_id(list.get(0).getFamily_member().getMembername());

			rs.setAttribute("list", list);
			process(rs, response, "/page/bx_record/print.jsp");
		} catch (DbException e) {
			error(rs, response);
		}
	}

	@SuppressWarnings("null")
	private void findByIdCard(HttpServletRequest rs, HttpServletResponse response)
			throws ServletException, IOException {
		String IDcard = rs.getParameter("IDcard");
		Join_nhDao join_nhDao = new Join_nhDao();
		Family_memberDAO family_memberDAO = new Family_memberDAO();
		Ill_cardDao ill_cardDao = new Ill_cardDao();
		List<Object> dataList = new ArrayList<Object>();
		HashMap<String, Object> dataMap = new HashMap<>();
		try {
			List<Family_member> list = family_memberDAO.findByKeyValue("IDcard", IDcard);
			List<Join_nh> list1 = join_nhDao.findJoin_nhList(IDcard);
			List<Ill_card> list2 = ill_cardDao.findByKeyValue("IDcard", IDcard);
			dataMap.put("membername", list.get(0).getMembername());
			dataMap.put("IDcard", list.get(0).getIDcard());
			dataMap.put("ill_card_no", list2.isEmpty() ? "无" : list2.get(0).getIll_card_no());// list2.get(0).getIll_card_no()
			dataMap.put("age", list.get(0).getAge());
			dataMap.put("occupation", list.get(0).getOccupation());
			dataMap.put("join_nh_code", list.get(0).getJoin_nh_code());
			dataMap.put("status", list1.isEmpty() ? "未参合" : "已参合");
			dataList.add(dataMap);
			// System.out.println(dataList);
			rs.setAttribute("list", dataList);
		} catch (DbException e) {
			error(rs, response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// 接收页面传递过来的参数
		String bx_record_code = request.getParameter("bx_record_code");
		String user_id = request.getParameter("user_id");
		String chronicdis_code = request.getParameter("chronicdis_code");
		String id_card = request.getParameter("id_card");
		String all_cost = request.getParameter("all_cost");
		String bx_cost = request.getParameter("bx_cost");
		String vis_time1 = request.getParameter("vis_time");
		Timestamp vis_time = null;
		vis_time = Timestamp.valueOf(vis_time1);
		String bx_time = request.getParameter("bx_time");
		String receipt = request.getParameter("receipt");
		String vis_city = request.getParameter("vis_city");
		String area_code = request.getParameter("area_code");

		String area = request.getParameter("area");
		String policy = request.getParameter("policy");
		String chronicdis = request.getParameter("chronicdis");
		String family_member = request.getParameter("family_member");
		String join_nh = request.getParameter("join_nh");
		String medical = request.getParameter("medical");

		// 实例化Family类
		BX_record bX_record = new BX_record();
		// 调用添加方法
		try {
			bX_record.addBX_record(bx_record_code, user_id, chronicdis_code, id_card, all_cost, bx_cost, vis_time,
					vis_city, area_code, request);
		} catch (Exception e) {
			error(request, response);
		}

	}

	private void edit(HttpServletRequest rs, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数

		String familycode = rs.getParameter("familycode");
		String familyname = rs.getParameter("familyname");
		String IDcard = rs.getParameter("IDcard");
		String familyaddr = rs.getParameter("familyaddr");
		String familynumber = rs.getParameter("familynumber");
		String familystate = rs.getParameter("familystate");
		String areacode = rs.getParameter("areacode");
		String familyteam = rs.getParameter("familyteam");

		try {
			// 2:构造新慢病信息对象
			Area area = new Area(areacode);
			Family model = new Family(familycode, familyname, IDcard, familyaddr, familynumber, familystate, area,
					familyteam);
			// 3：调用保存的方法
			model.edit();
			process(rs, resp, "/page/family/family_list.jsp");
		} catch (Exception e) {
			error(rs, resp);
		}

	}

	private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数
		String familycode = req.getParameter("id");

		// 2:构造新慢病信息对象
		Family model = new Family();
		try {
			model.del(familycode);
			process(req, resp, "/page/family/family_list.jsp");
		} catch (Exception e) {
			error(req, resp);
		}

	}

}
